
package vu.cs.heuristieken.gui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import javax.swing.JFrame;
import vu.cs.heuristieken.Field;
import vu.cs.heuristieken.Position;
import vu.cs.heuristieken.Placement;


public class GuiWindow extends JFrame implements KeyListener {
    private int                         currentFieldIndex;
    private int                         currentSubfieldIndex;
    private List<Field>                 fields;
    private List<List<List<Placement>>> subFields;
    private FieldDisplayer              fieldDisplayer;

    public GuiWindow() {
        this.currentFieldIndex    = 0;
        this.currentSubfieldIndex = 0;
        this.fieldDisplayer       = new FieldDisplayer();

        Container contentPane     = this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(this.fieldDisplayer, BorderLayout.CENTER);
        this.validate();

        this.addKeyListener(this);
        contentPane.addKeyListener(this);
        this.fieldDisplayer.addKeyListener(this);

        this.pack();
        this.setVisible(true);
    }

    public void showFields(List<Field> fields, List<List<List<Placement>>> subFields) {
        this.fields               = fields;
        this.subFields            = subFields;
        this.currentFieldIndex    = 0;
        this.currentSubfieldIndex = 0;
        if(0 != this.fields.size()) {
            this._showField(this.fields.get(this.currentFieldIndex), this.subFields.get(this.currentFieldIndex).get(this.currentSubfieldIndex));
        }
    }

    private void _showField(Field field, List<Placement> subFields) {
        this.fieldDisplayer.showField(field, subFields);

        Dimension preferredSize = this.fieldDisplayer.getPreferredSize();
        this.setPreferredSize(new Dimension((int) preferredSize.getWidth() + 10, (int) preferredSize.getHeight() + 40));
        this.pack();
    }

    public void keyTyped(KeyEvent event) {
        // Do nothing
    }

    public void keyPressed(KeyEvent event) {
        int nSubfields = 0;

        switch( event.getKeyCode()) {
        case KeyEvent.VK_LEFT:
            this.currentFieldIndex    = (0 == this.currentFieldIndex)? this.fields.size() - 1 : this.currentFieldIndex - 1;
            this.currentSubfieldIndex = 0;
            break;
        case KeyEvent.VK_RIGHT:
            this.currentFieldIndex    = ((this.fields.size() - 1) == this.currentFieldIndex)? 0 : this.currentFieldIndex + 1;
            this.currentSubfieldIndex = 0;
            break;
        case KeyEvent.VK_UP:
            nSubfields = this.subFields.get(this.currentFieldIndex).size();
            this.currentSubfieldIndex = ((nSubfields - 1) == this.currentSubfieldIndex)? 0 : this.currentSubfieldIndex + 1;
            break;
        case KeyEvent.VK_DOWN:
            nSubfields = this.subFields.get(this.currentFieldIndex).size();
            this.currentSubfieldIndex = (0 == this.currentSubfieldIndex)? nSubfields - 1 : this.currentSubfieldIndex - 1;
            break;
        default:
            return;
        }
        System.out.println("Moving to field " + currentFieldIndex + "/" + this.fields.size());

        this._showField(this.fields.get(this.currentFieldIndex), this.subFields.get(this.currentFieldIndex).get(this.currentSubfieldIndex));
    }

    public void keyReleased(KeyEvent event) {
        // Do nothing
    }
}